package com.hanoi.wiki.controller.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanoi.wiki.controller.CoordinatesController;
import com.hanoi.wiki.exception.FunctionalException;
import com.hanoi.wiki.manager.CoordinatesManager;
import com.hanoi.wiki.model.json.BaseJsonObject;
import com.hanoi.wiki.model.json.CoordinatesJson;
import com.hanoi.wiki.model.json.ResponseJsonObject;

@Controller
@RequestMapping("/cdn")
public class CoordinatesControllerImpl extends BaseControllerImpl implements CoordinatesController{

	@Autowired
	private CoordinatesManager coordinatesManager;
	
	public @ResponseBody ResponseJsonObject saveCoordinates(@RequestBody CoordinatesJson coordinates) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		int id = 0;
		try {
			id = coordinatesManager.save(coordinates);
		} catch (FunctionalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		coordinates.setId(id);
		objects.add(coordinates);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject removeCoordinates(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		CoordinatesJson json = null;
		try {
			json = coordinatesManager.get(id);
			coordinatesManager.remove(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject updateCoordinates(@RequestBody CoordinatesJson coordinates) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			coordinatesManager.update(coordinates);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(coordinates);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getCoordinates(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		CoordinatesJson json = null;
		try {
			json = coordinatesManager.get(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getCoordinatesList() {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			objects.addAll(coordinatesManager.getList());
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	
	
}
